﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Data.Linq;
using System.Reflection;
using System.Data.Linq.Mapping;
using System.Net;


namespace DAL_Class
{
    public class HocVien : NLLDataContext
    {
        NLLDataContext db = new NLLDataContext();
        public int? AddNewCheckIn(string fullname, string phone, DateTime birthday, string email, string company, string note)
        {
            string result = string.Empty;
            System.Nullable<int> p1 = null;
            try
            {
                var q = db.sp_AddNewCheckIn(ref p1, fullname, phone, birthday, email, company, note);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return p1;
        }

        public string AddNewReg(int checkID, int courseID)
        {
            try
            {
                string result = string.Empty;
                var q = db.sp_AddNewReg(checkID, courseID);
                result = "Đăng kí thành công!";
                return result;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
        }

        public void updateCheckIn(CheckIn ent)
        {
            try
            {
                CheckIn ci = db.CheckIns.Single(p => p.ID == ent.ID);
                ci.FullName = ent.FullName;
                ci.Phone = ent.Phone;
                ci.Email = ent.Email;
                ci.DateOfBirth = ent.DateOfBirth;
                ci.Company = ent.Company;
                ci.Note = ent.Note;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            
        }

        #region "Xóa khóa học mà học viên đã đăng kí"
        public void DeleteRegisterCourse(int ID)
        {
            try
            {
                var DelRegCou = from p in db.RegisterCourses
                                where p.ID.Equals(ID)
                                select p;
                db.RegisterCourses.DeleteAllOnSubmit(DelRegCou);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }   
        }            
        #endregion

        #region "Xóa học viên"
        public void deleteCheckIn(int ID)
        { 
            try
            {
                var DelRegCou = from p in db.RegisterCourses
                                where p.CheckInID.Equals(ID)
                                select p;
                db.RegisterCourses.DeleteAllOnSubmit(DelRegCou);
                db.SubmitChanges();

                var DelCheckIn = from c in db.CheckIns
                                 where c.ID.Equals(ID)
                                 select c;
                db.CheckIns.DeleteAllOnSubmit(DelCheckIn);
                db.SubmitChanges();

                
            }
            catch (Exception ex)
            {
                throw ex;
            }   
        }
        #endregion

        #region "Gửi email thông tin học viên đăng kí cho admin"
        public bool SendEmail(string tittle, string details)
        {
            try
            {
                SmtpEmail sms = db.SmtpEmails.Single(sm => sm.ID == 1);
                string mailFrom = sms.MailFrom;
                string mailTo = sms.MailTo;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(sms.SmtpClient);
                System.Net.Mail.MailMessage mess = new System.Net.Mail.MailMessage(mailFrom, mailTo, tittle, details);
                mess.IsBodyHtml = true;
                smtp.EnableSsl = true;
                smtp.Host = sms.SmtpHost;
                smtp.Port = sms.SmtpPort;
                smtp.Credentials = new NetworkCredential(sms.MailFrom, sms.MailFromPassword);
                smtp.Send(mess);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region "Lay thong tin config send mail"
        public SmtpEmail GetConfigMail()
        {
 
                SmtpEmail sms = db.SmtpEmails.Single(sm => sm.ID == 1);
                return sms;

        }
        #endregion

        #region "Thay doi config send mail"
        public string ChangConfigMail(SmtpEmail sms)
        {

           // 
            try
            {
                SmtpEmail s= db.SmtpEmails.Single(sm => sm.ID == sms.ID);

                s.MailFrom = sms.MailFrom;
                s.MailFromPassword = sms.MailFromPassword;
                s.MailTo = sms.MailTo;
                s.SmtpClient = sms.SmtpClient;
                s.SmtpHost = sms.SmtpHost;
                s.SmtpPort = sms.SmtpPort;
                db.SubmitChanges();
                return "Cập nhật thành công";
            }
            catch (Exception)
            {
                
                throw;
                return "Lỗi xảy ra với hệ thống vui lòng liên hệ quản trị viên.";
            }

        }
        #endregion

    }

}
